package com.ls.demo.bean;

import com.ls.demo.tcp.namespace.Namespaces;
import io.netty.channel.ChannelHandlerContext;
import io.netty.util.AttributeKey;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.util.UUID;

/**
 * @author liShan
 * @date 2019/9/4 14:23
 */
@Getter
@Setter
@ToString
public class RoomBoxInfo extends BaseInfo {

    private String boxId;
    private Long roomId;
    /**
     * 房间栋号
     */
    private int buildingNum;

    /**
     * 酒店号
     */
    private int hotelNum;

    /**
     * 楼层号
     */
    private int layerNum;

    /**
     * 房间号
     */
    private int roomNum;


    public RoomBoxInfo() {
    }

    private RoomBoxInfo(Builder builder) {
        this.roomId = builder.roomId;
        this.boxId = builder.boxId;
        this.roomNum = builder.roomNum;
        this.buildingNum = builder.buildingNum;
        this.hotelNum = builder.hotelNum;
        this.layerNum = builder.layerNum;
    }

    public static Builder builder() {
        return new Builder();
    }

    /**
     * 获取盒子实例
     * @param ctx 通道上下文
     * @param command 命令
     * @param namespaces 连接空间
     * @return 盒子实例
     */
    public static RoomBoxInfo getRoomBoxInfo(ChannelHandlerContext ctx,
                                             Command command,
                                             Namespaces namespaces){
        AttributeKey<UUID> id = AttributeKey.valueOf("id");
        String uuid = ctx.channel().attr(id).get().toString();
        return RoomBoxInfo.builder()
                .hotelNum(command.getHotelDec())
                .buildingNum(command.getBuildDec())
                .layerNum(command.getLayerDec())
                .roomNum(command.getRoomDec())
                .boxId(namespaces.getBoxUuid(uuid))
                .build();
    }

    /**
     * 获取酒店号的高8位
     * @return 高8位
     */
    public int getHotelHigh(){
        return this.hotelNum >> 8 ;
    }

    /**
     * 获取酒店号的高8位
     * @return 高8位
     */
    public int getHotelLow(){
        return this.hotelNum & 255 ;
    }

    public static class Builder extends BaseBuilder<Builder>{

        private String boxId;
        private Long roomId;
        private int buildingNum;
        private int hotelNum;
        private int layerNum;
        private int roomNum;

        @Override
        Builder self() {
            return this;
        }

        public Builder boxId(String boxId){
            this.boxId = boxId;
            return this;
        }

        public Builder roomId(Long roomId){
            this.roomId = roomId;
            return this;
        }

        public Builder layerNum(int layerNum){
            this.layerNum = layerNum;
            return this;
        }

        public Builder buildingNum(int buildingNum){
            this.buildingNum = buildingNum;
            return this;
        }

        public Builder roomNum(int roomNum){
            this.roomNum = roomNum;
            return this;
        }

        public Builder hotelNum(int hotelNum){
            this.hotelNum = hotelNum;
            return this;
        }

        public RoomBoxInfo build() {
            return new RoomBoxInfo(this);
        }

    }

}
